package com.code.splider.controller;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xml.sax.SAXException;

import com.code.splider.service.ScoreService;

@Controller
@RequestMapping("score")
public class ScoreController {

	@Autowired
	private ScoreService scoreService;

	@RequestMapping(value = "save")
	public String save() throws IOException, SAXException {
		System.out.println("向服务器发送数据，然后获取网页内容：");
		try {
			Document doc = Jsoup.connect("http://live.dszuqiu.com/ajax/score/data?mt=0&nr=1")
					.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/22.0")
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.ignoreContentType(true).timeout(5000).get();
			Element jsonText = doc.select("body").first();
			if (null != jsonText) {
				scoreService.saveScore(jsonText.html());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "result";
	}

}
